#include "LogController.h"

LogController::LogController(void)
{
}

LogController::~LogController(void)
{
}

void	LogController::AddToLog(LogController::LogLevel logLevel, std::string stringLog)
{
	_mutex.lock();
	if (_logFileStream.is_open()) //si le fichier est ouvert
	{
		_logFileStream << "[" << getTimeFormat() << "]";
		switch (logLevel)
		{
		case L_NOTICE:
			{
				_logFileStream << "[Notice]	";
			}break;
		case L_WARNING:
			{
				_logFileStream << "[Warning]	";
			}break;
		case L_FAILURE:
			{
				_logFileStream << "[Failure]	";
			}break;
		case L_ERROR:
			{
				_logFileStream << "[Error]	";
			}break;
		case L_CRITICAL:
			{
				_logFileStream << "[Critical]	";
			}break;
		case L_SOCKET:
			{
				_logFileStream << "[Socket]	";
			}break;
		}
		_logFileStream << stringLog << std::endl;
	}
	_mutex.unlock();
}

void	LogController::startSession(std::string fileName)
{
	_mutex.lock();
	this->_logFileName = fileName;

	_logFileStream.open(_logFileName, std::ios::app);

	if (_logFileStream.is_open())
	{
		_logFileStream << "###################################" << std::endl;
		_logFileStream << "###	NEW SESSION STARTED			###" << std::endl;
		_logFileStream << "###	Date : " << getTimeFormat() << "	###" << std::endl;
		_logFileStream << "###	Session type : DEBUG		###" << std::endl;
		_logFileStream << "###################################" << std::endl;
	}
	_mutex.unlock();
}

void LogController::stopSession()
{
	_mutex.lock();
	if (_logFileStream.is_open())
	{
		_logFileStream << "### Session ended at " << getTimeFormat() << " ###" << std::endl;
		_logFileStream.close();
	}
	_mutex.unlock();
}

std::string LogController::getTimeFormat()
{
	std::string temp;
	time_t rawtime;
	struct tm *timeinfo;
	char buffer [80];

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	strftime (buffer,80,"%c",timeinfo);
	temp = buffer;
	return temp;
}